package me.exchange.com.modules.gen.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.exchange.com.annotation.AnonymousAccess;
import me.exchange.com.modules.gen.domain.ColumnInfo;
import me.exchange.com.modules.gen.domain.GenConfig;
import me.exchange.com.modules.gen.service.GenConfigService;
import me.exchange.com.modules.gen.service.GeneratorService;
import me.exchange.com.utils.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author Zheng Jie
 * @date 2019-01-02
 */
@RestController
@RequestMapping("/api/generator")
@Api(tags = "系统：代码生成管理")
public class GeneratorController {

    private final GeneratorService generatorService;

    private final GenConfigService genConfigService;

    @Value("${generator.enabled}")
    private Boolean generatorEnabled;

    public GeneratorController(GeneratorService generatorService, GenConfigService genConfigService) {
        this.generatorService = generatorService;
        this.genConfigService = genConfigService;
    }
//生成代码
    @ApiOperation("Api生成代码")
    @PostMapping(value = "/generatorCode")
    @AnonymousAccess
    public ResponseEntity<Object> apiGenerator(@RequestParam Map<String,Object> map, HttpServletRequest request, HttpServletResponse response){
        GenConfig config = new GenConfig();
        config.setId(1L);//主键
        config.setTableRemark(map.get("tableRemark").toString());//表注释
        config.setTableName(map.get("tableName").toString());//表名
        config.setPrefix(map.get("prefix").toString());//表前缀
        config.setAuthor("zjj");//作者
        config.setModuleName("exchange-service");//模块
        config.setPack("me.exchange.com.modules.system");//生成包名
        config.setApiAlias(map.get("apiName").toString());//接口名称
        config.setCover(false);//是否覆盖
        List<ColumnInfo> columns =generatorService.getColumnses(map.get("tableName").toString());
        generatorService.generator(config, columns);
        return new ResponseEntity<>(Result.ok("Api生成代码成功"),HttpStatus.OK);
    }
}
